// Fhourstones 3.0 Board Logic
// (http://www.cwi.nl/~tromp/c4/fhour.html)
//
// implementation of the well-known game
// usually played on a vertical board of 7 columns by 6 rows,
// where 2 players take turns in dropping counters in a column.
// the first player to get four of his counters
// in a horizontal, vertical or diagonal row, wins the game.
// if neither player has won after 42 moves, then the game is drawn.
//
// This software is copyright (c) 1996-2008 by
//      John Tromp
//      600 Route 25A
//      East Setauket
//      NY 11733
// E-mail: john.tromp@gmail.com
//
// This notice must not be removed.
// This software must not be sold for profit.
// You may redistribute if your distributees have the
// same rights and restrictions.

//The original bitboard was split up into two files and added to.

/***********************************************************************
 ** Reykjavik University
 ** Grímur Tómasson & Eiríkur Fannar Torfason
 ** Copyright (c) 2011. All rights reserved.
 **********************************************************************/

#ifndef BITBOARD_
#define BITBOARD_


#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>

//----------------------------------------------------------------------------------------------------------

#define WIDTH 7
#define HEIGHT 6
// bitmask corresponds to board as follows in 7x6 case:
//  .  .  .  .  .  .  .  TOP
//  5 12 19 26 33 40 47
//  4 11 18 25 32 39 46
//  3 10 17 24 31 38 45
//  2  9 16 23 30 37 44
//  1  8 15 22 29 36 43
//  0  7 14 21 28 35 42  BOTTOM

#define H1 (HEIGHT+1)
#define H2 (HEIGHT+2)
#define SIZE (HEIGHT*WIDTH)
#define SIZE1 (H1*WIDTH)

typedef u_int64_t uint64;
typedef uint64 bitboard;

#define COL1 (((bitboard)1<<H1)-(bitboard)1)

#if (SIZE1 != 64)
#define ALL1 (((bitboard)1<<SIZE1)-(bitboard)1)
#define BOTTOM (ALL1 / COL1) // has bits i*H1 set
#elif (WIDTH==8 && HEIGHT==7) // one of exceptional cases
#define BOTTOM 0x0101010101010101LL
#endif

#define TOP (BOTTOM << HEIGHT)

//Added
#define CHECK_BIT(var,pos)((var) & (1LL << (pos)))
#define SET_BIT(var,pos)((var) |= (1LL << (pos)))

const char COLOR_O_ALIAS = 'B';
const char COLOR_1_ALIAS = 'R';
//----------------------------------------------------------------------------------------------------------
//Functions

bitboard checkBit( bitboard board, bitboard position );
void reset();
void printMoves();
int isLegal(bitboard newboard);
int isPlayable(int col);
bitboard hasWon(bitboard newboard);
int isLegalHasWon(bitboard newboard);
void undoMove();
void makeMove(int n);
bitboard newBoardAndMove( bitboard original, int column );
int getCellIndex(int column, int row);
char getSymbol( const bitboard bboard1, const bitboard bboard2, int index );
void printStatus();
void printStatusInline();
void printDebug(bitboard board);
void move( int column );
int* ply();
bitboard* colorPointer(int index);
int firstLegalMove();
//----------------------------------------------------------------------------------------------------------

#endif